{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7ecae9a4-2927-47a2-ba0a-723ebb6dda96",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Setup OpenAI Agent\n",
    "from llama_index.agent import OpenAIAgent\n",
    "import openai\n",
    "\n",
    "openai.api_key = \"sk-your-key\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6f564f5-5c92-4ca7-8925-54989870deab",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import and initialize our tool spec\n",
    "from llama_index.tools.wikipedia.base import WikipediaToolSpec\n",
    "from llama_index.tools.tool_spec.load_and_search.base import LoadAndSearchToolSpec\n",
    "\n",
    "wiki_spec = WikipediaToolSpec()\n",
    "# Get the search wikipedia tool\n",
    "tool = wiki_spec.to_tool_list()[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9137752b-bd7f-48bc-a401-f2f440ad2f49",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create the Agent with our tools\n",
    "agent = OpenAIAgent.from_tools(\n",
    "    LoadAndSearchToolSpec.from_defaults(tool).to_tool_list(), verbose=True\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a8f7351a-9b53-4f80-9276-69b4a2aa6008",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== Calling Function ===\n",
      "Calling function: search_data with args: {\n",
      "  \"query\": \"capital of Poland\"\n",
      "}\n",
      "Got output: Content loaded! You can now search the information using read_search_data\n",
      "========================\n",
      "=== Calling Function ===\n",
      "Calling function: read_search_data with args: {\n",
      "  \"query\": \"What is the capital of Poland?\"\n",
      "}\n",
      "Got output: \n",
      "The capital of Poland is Warsaw.\n",
      "========================\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Response(response='The capital of Poland is Warsaw.', source_nodes=[], metadata=None)"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent.chat(\"what is the capital of poland\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "888a8712-6712-472b-aca1-4b08acceb754",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== Calling Function ===\n",
      "Calling function: search_data with args: {\n",
      "  \"query\": \"history of Poland\"\n",
      "}\n",
      "Got output: Content loaded! You can now search the information using read_search_data\n",
      "========================\n",
      "=== Calling Function ===\n",
      "Calling function: read_search_data with args: {\n",
      "  \"query\": \"How long has Poland existed?\"\n",
      "}\n",
      "Got output: \n",
      "Poland has existed for over a thousand years, since the 10th century when Duke Mieszko I established the Piast dynasty.\n",
      "========================\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Response(response='Poland has existed for over a thousand years, since the 10th century when Duke Mieszko I established the Piast dynasty.', source_nodes=[], metadata=None)"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent.chat(\"how long has poland existed\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "37155aa5-15d6-4335-9e6e-3a275556a463",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== Calling Function ===\n",
      "Calling function: read_search_data with args: {\n",
      "  \"query\": \"What is the size of Poland?\"\n",
      "}\n",
      "Got output: \n",
      "Poland has an area of 312,696 km2 (120,733 sq mi).\n",
      "========================\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Response(response='Poland has an area of 312,696 km2 (120,733 sq mi).', source_nodes=[], metadata=None)"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent.chat(\"using information already loaded, how big is poland?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "857696e1-7a5e-4dad-9101-397f947e619d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== Calling Function ===\n",
      "Calling function: read_search_data with args: {\n",
      "  \"query\": \"What is the size of Greece?\"\n",
      "}\n",
      "Got output: \n",
      "Greece has a GDP per capita of approximately $16,000, which is on par with Portugal and the regions of Spain and Greece.\n",
      "========================\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Response(response=\"Apologies, but I couldn't find the information about the size of Greece in the loaded data.\", source_nodes=[], metadata=None)"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent.chat(\"using information already loaded, how big is greece?\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
